package com.mumu.coroutinenewdemo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

public class ScrollingActivity extends AppCompatActivity implements DeviceKeyMonitor.OnKeyListener {
    private ViewGroup group;
    private ImageView imageView;
    private DeviceKeyMonitor monitor;

    @Override
    public void onRecentClick() {
        group.removeView(imageView);
//      创建imageview
        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);

//      截图，并将图片高斯模糊处理
        Bitmap bitmap = PicUtil.rsBlur(this, PicUtil.activityShot(this), 20);
//      将处理后的图片填充到imageview
        imageView.setImageBitmap(bitmap);
        group.addView(imageView);

    }

    @Override
    protected void onResume() {
        super.onResume();
        for (int i = 0; i < group.getChildCount(); i++) {
            group.removeView(imageView);
        }

    }

    @Override
    protected void onDestroy() {
        monitor.unregister();
        super.onDestroy();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);

        monitor = new DeviceKeyMonitor(this, this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        toolBarLayout.setTitle(getTitle());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        group = (ViewGroup) getWindow().getDecorView();
    }

}